package com.ziyue.socket.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ziyue.socket.bean.Member;
import com.ziyue.socket.bean.Room;
import com.ziyue.socket.bean.SocketResponse;
import com.ziyue.socket.bean.answer.AnswerFinishRequest;
import com.ziyue.socket.bean.answer.AnswerRequest;
import com.ziyue.socket.bean.answer.AnswerRightResponse;
import com.ziyue.socket.cache.StudentFinishDateService;
import com.ziyue.socket.enums.OptionEnum;
import com.ziyue.socket.enums.RoleEnum;
import com.ziyue.socket.enums.SocketStatusEnum;
import com.ziyue.socket.exception.IllegalOperationMessageException;
import com.ziyue.socket.service.OptionAbstract;
import io.netty.channel.Channel;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.ziyue.socket.enums.RoleEnum.STUDENT;

/**
 * Created by wancheng on 2019/4/18.
 */
public class AnswerFinish extends OptionAbstract {

    private final OptionEnum OPTION = OptionEnum.ANSWER_FINISH;

    @Override
    public Object send(Channel channel, String requestBody) throws Exception {
        AnswerFinishRequest request = JSONObject.parseObject(requestBody, AnswerFinishRequest.class);
        StudentFinishDateService.setStudentFinishDate(channel,request.getCurrentDate());
        Room room = roomManage.getRoom(channel);
        Member member = roomManage.getMember(channel.id());
        List<AnswerRightResponse> right = getRight(room);
        Optional<AnswerRightResponse> memberAnswerOptional = right.stream()
                .filter(a->a.getAccount().equals(member.getAccount()))
                .findFirst();
        AnswerRightResponse memberAnswer = memberAnswerOptional.get();
        sendMsg(channel,"当前学生作答情况",memberAnswer,OPTION);

        Map<String,Date> studentFinishDateMap = StudentFinishDateService.getStudentFinishDate(room);
        List<Member> students = room.getRoleMembers(RoleEnum.STUDENT);
        Boolean ifAllStudentFinish = false;
        if (students.size() == studentFinishDateMap.keySet().size()){
            ifAllStudentFinish = true;
        }
        sendTeacher(channel,"学生是否全部作答完成",ifAllStudentFinish,OPTION);
        return null;
    }

    @Override
    public Object get(Channel channel, String requestBody) {
        return null;
    }
}
